package com.toocms.jinjuhang.ui.advert_list

import android.os.Bundle
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.VideoData
import com.toocms.jinjuhang.dialog.hint.HintDialog
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.ItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class AdvertListItemModel(viewModel: AdvertListModel, val item: ObservableField<VideoData>) :
    ItemViewModel<AdvertListModel>(viewModel) {

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply video@{
            val params = mutableMapOf<String, String?>()
            params["member_id"] = UserRepository.user.member_id
            params["video_id"] = video_id
            viewModel.requestData(
                Urls.Center.CHECK_VIDEO, params, resultClass = String::class.java, result = {
//            showToast(it)
                    val bundle = Bundle()
                    bundle.putString(Constants.BundleKey.KEY_VIDEO_ID, video_id)
                    bundle.putParcelable(Constants.BundleKey.KEY_VIDEO, this)
                    viewModel.startFragment(AdvertDetailsFgt::class.java, bundle)
                },
                error = {
                    HintDialog()
                        .apply {
                            title = StringUtils.getString(R.string.str_hint)
                            content = "观看此视频您已无法获得积分是否继续观看"
                            positive = StringUtils.getString(R.string.str_confirm)
                            passive = StringUtils.getString(R.string.str_cancel)
                            onPositiveClickListener = {
                                val bundle = Bundle()
                                bundle.putString(Constants.BundleKey.KEY_VIDEO_ID, video_id)
                                bundle.putParcelable(Constants.BundleKey.KEY_VIDEO, this@video)
                                viewModel.startFragment(AdvertDetailsFgt::class.java, bundle)
                            }
                            show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager,null)
                        }
                })

        }

    })

}